#include "exitbutton.h"

widget_t *create_exit_button(widget_t *parent, int32_t x, int32_t y,
    uint32_t width, uint32_t height)
{
    // create the widget
    widget_t *self = create_widget(parent,x,y,width,height,FALSE);
    // create exit button external information
    self->ext = malloc(sizeof(exit_button_t));
    ((exit_button_t *)self->ext)->pressed = FALSE;
    // connect the callback functions
    callback_t *callback = self->callback;
    callback->paint = exit_button_paint;
    callback->lbutton_down = exit_button_lbutton_down;
    callback->lbutton_up = exit_button_lbutton_up;
    callback->mouse_enter = exit_button_mouse_enter;
    callback->mouse_leave = exit_button_mouse_leave;
    callback->free = exit_button_free;
    // return pointer to the created widget
    return self;
}

void exit_button_paint(widget_t *self, HDC hdc)
{
    exit_button_t *ext = self->ext;
    // get the rect of the client
    RECT rect;
    GetClientRect(self->hwnd,&rect);
    // draw the background gray/red depending if the mouse have entered or not
    if(self->mouse_entered_flag == TRUE) {
        HBRUSH red = CreateSolidBrush(RGB(200,0,0));
        FillRect(hdc, &rect, red);
        DeleteObject(red);
    }
    else {
        HBRUSH grey = CreateSolidBrush(RGB(200,200,200));
        FillRect(hdc, &rect, grey);
        DeleteObject(grey);
    }
    // create and select a white pen
    HPEN pen = CreatePen(PS_SOLID, 2, RGB(0,0,0));
    SelectObject(hdc, pen);
    // Draw the cross
    MoveToEx(hdc, rect.left+5, rect.top+5, NULL);
    LineTo(hdc, rect.right-5, rect.bottom-5);
    MoveToEx(hdc, rect.right-5, rect.top+5, NULL);
    LineTo(hdc, rect.left+5, rect.bottom-5);
    // remove the pen object
    DeleteObject(pen);
    // draw the button raised or sunken dependet if the button is pressed
    if(ext->pressed == TRUE) {
        DrawEdge(hdc, &rect, EDGE_SUNKEN, BF_RECT);
    }
    else {
        DrawEdge(hdc, &rect, EDGE_RAISED, BF_RECT);
    }
}

void exit_button_lbutton_down(widget_t *self, int32_t x, int32_t y,
    uint32_t flag)
{
    exit_button_t *ext = self->ext;
    ext->pressed = TRUE;
    // redraw the widget
    InvalidateRect(self->hwnd, NULL, FALSE);
}


void exit_button_lbutton_up(widget_t *self, int32_t x, int32_t y, uint32_t flag)
{
    exit_button_t *ext = self->ext;
    ext->pressed = FALSE;
    // redraw widget
    InvalidateRect(self->hwnd, NULL, FALSE);
    // signal that this button have been clicked
    SIGNAL(self, CLICKED, NULL);
}

void exit_button_mouse_enter(widget_t *self, int32_t x, int32_t y,
    uint32_t flag)
{
    // redraw widget
    InvalidateRect(self->hwnd, NULL, FALSE);
}

void exit_button_mouse_leave(widget_t *self)
{
    exit_button_t *ext = self->ext;
    ext->pressed = FALSE;
    // redraw widget
    InvalidateRect(self->hwnd, NULL, FALSE);
}

void exit_button_free(widget_t *self)
{
    free(self->ext);
    free(self);
}

